
package cn.xiaobai.loader;

import java.lang.reflect.Method;

public class MainMethodRunner {
    private final String mainClassName;

    private final Class<?> aClass;

    private final String[] args;

    public MainMethodRunner(String mainClassStr, Class<?> mainClass, String[] args) {
        this.mainClassName = mainClassStr;
        this.aClass = mainClass;
        this.args = args != null ? args.clone() : null;
    }

    public void run() throws Exception {
        Class<?> mainClass = mainClassName != null
                ? Class.forName(this.mainClassName, false, Thread.currentThread().getContextClassLoader())
                : aClass;
        if (mainClass == null) {
            throw new IllegalStateException("未找到主类清单!");
        }

        Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
        mainMethod.setAccessible(true);
        mainMethod.invoke(null, (Object) this.args);

    }
}
